<template>
  <div :class="className" :style="{'height':'100%','width':'100%'}" />
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from '../../views/dashboard/mixins/resize';
  const animationDuration = 1000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '500px'
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el)
        this.setOption(this.chartData)
      },
      setOption({xdata,ydata1,ydata2}={}){
        console.log(xdata)
        this.chart.setOption({
          textStyle:{
            fontSize: 16,
            fontWeight:400,
            color:'#fff'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            },
            valueFormatter: (value) => Math.abs(value)
          },
          legend: {
            textStyle:{
              fontSize: 12,
              fontWeight:400,
              color:'#fff'
            },
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            top:40,
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: xdata
            }
          ],
          yAxis: [
            {
              type: 'value',
              axisTick: {
                show: false
              },
              axisLabel:{
                formatter: function (value, index) {
                    return Math.abs(value);
                }
              },
            }
          ],
          series: [
            {
              name: this.$t("index.chargingCapacity"),
              type: 'bar',
              stack: 'Total',
              label: {
                show: true,
                textStyle:{
                  fontSize: 12,
                  fontWeight:400,
                  color:'#fff'
                },
              },
              emphasis: {
                focus: 'series'
              },
              data: ydata1
            },
            {
              name: this.$t("index.dischargingCapacity"),
              type: 'bar',
              stack: 'Total',
              label: {
                show: true,
                textStyle:{
                  fontSize: 12,
                  fontWeight:400,
                  color:'#fff'
                },
                formatter: function (value, index) {
                    return Math.abs(value.value);
                }
                // position: 'bottom'
              },
              emphasis: {
                focus: 'series'
              },
              data: ydata2
            }
          ]
        })
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOption(val)
        }
      }
    },
  }
</script>
